package com.hutao.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.BaseInfoMapper;
import com.hutao.manage.mapper.BasicInfoMapper;
import com.hutao.manage.mapper.StdDiseaseMapper;
import com.hutao.manage.mapper.StdOperationMapper;
import com.hutao.manage.service.BasicInfoService;

@Service
public class BasicInfoServiceImpl implements BasicInfoService {
	
	@Autowired
	private StdDiseaseMapper stdDiseaseMapper;
	
	@Autowired
	private StdOperationMapper stdOperationMapper;
		
	@Autowired
	private BasicInfoMapper baseInfoMapper;
	
	@Autowired
	private BaseInfoMapper baseInfo1Mapper;

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getSimpleBasicInfos() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> basicInfoMap = new HashMap<String, List<Map<String,Object>>>();
		basicInfoMap.put("sexes", baseInfoMapper.findSex());
		basicInfoMap.put("marriages", baseInfoMapper.getMarriage());
		basicInfoMap.put("outHospitalTypes", baseInfoMapper.getOutHospitalTypes());
		return basicInfoMap;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBasicInfos() {
		Map<String, List<Map<String, Object>>> basicInfoMap = new HashMap<String, List<Map<String,Object>>>();
		basicInfoMap.put("sexes", baseInfoMapper.findSex());
		basicInfoMap.put("hospitals", baseInfoMapper.getHospitals());
		basicInfoMap.put("marriages", baseInfoMapper.getMarriage());
		basicInfoMap.put("offices",baseInfoMapper.getMedicalOfficesByHospitalId(1));
		
		basicInfoMap.put("inHospitalList", baseInfoMapper.getInHospitalTypes());	
		basicInfoMap.put("outHospitalList", baseInfoMapper.getOutHospitalTypes());		
		basicInfoMap.put("medicalPayTypeList", baseInfoMapper.getMedicalPayType());	
		basicInfoMap.put("professionList", baseInfoMapper.getProfessions());		
		basicInfoMap.put("countryList", baseInfoMapper.getCountrys());	
		basicInfoMap.put("nationList", baseInfoMapper.getNations());
		
		basicInfoMap.put("relationList", baseInfoMapper.getpatientRelations());
		
		basicInfoMap.put("identityDocumentTypeList", baseInfoMapper.getIdentityDocumentTypes());	
		basicInfoMap.put("inHospitalStateList", baseInfoMapper.getInHospitalStates());	
		basicInfoMap.put("deathReasonList", baseInfoMapper.getDeathReasons());	
		
		//再住院目的
		basicInfoMap.put("rehospitalizationAims", baseInfoMapper.getRehospitalizationAims());	
		//再手术目的
		basicInfoMap.put("reoperationAims", baseInfoMapper.getReoperationAims());	
		
		//诊断符合类型
		basicInfoMap.put("diagnosticCoincidenceTypes", baseInfoMapper.getDiagnosticCoincidenceTypes());	
		
		//ICU类型
		basicInfoMap.put("iCUTypes", baseInfoMapper.getICUTypes());	
		
		//过敏药物类型
		basicInfoMap.put("drugAllergyTypes", baseInfoMapper.getDrugAllergyTypes());	
		
		
		//压疮来源
		basicInfoMap.put("pressureSoreCradles", baseInfoMapper.getPressureSoreCradles());	
		//压疮分期
		basicInfoMap.put("pressureSorePhases", baseInfoMapper.getPressureSorePhases());	
		//压疮部位
		basicInfoMap.put("pressureSoreParts", baseInfoMapper.getPressureSoreParts());	
		
		//疾病诊断信息
		basicInfoMap.put("diagnoseTypeList", baseInfoMapper.getDiagnoseTypes());
		basicInfoMap.put("inHospitalDiseaseStateList", baseInfoMapper.getinHospitalDiseaseStates());
		basicInfoMap.put("treatmentResultList", baseInfoMapper.getTreamentResults());
		
		//治疗信息
		basicInfoMap.put("bloodTypeList", baseInfoMapper.getBloodTypes());
		basicInfoMap.put("rhBloodTypeList", baseInfoMapper.getRHBloodTypes());
		
		//手术操作
		basicInfoMap.put("incidionLevelList", baseInfoMapper.getIncisionLevels());
		basicInfoMap.put("cicatrizationTypeList", baseInfoMapper.getStdCicatrizationTypes());
		basicInfoMap.put("opsMarkList", baseInfoMapper.getOperationMarks());
		basicInfoMap.put("opsTimeLimitList", baseInfoMapper.getOpsTimeLimits());
		basicInfoMap.put("opsLevelList", baseInfoMapper.getOpsLevels());
		basicInfoMap.put("opsPartList", baseInfoMapper.getOpsParts());
		basicInfoMap.put("opsInfectPartList", baseInfoMapper.getOpsInfectParts());
		basicInfoMap.put("opsAfterSyndromList", baseInfoMapper.getOpsAfterSyndroms());
		basicInfoMap.put("anaesthesiaLevelList", baseInfoMapper.getAnaesthesiaLevels());
		basicInfoMap.put("anaesthesiaWayList", baseInfoMapper.getAnaesthesiaWays());
		basicInfoMap.put("opsAfterDirectionList", baseInfoMapper.getOpsAfterDirections());
		basicInfoMap.put("opsRiskLevelList", baseInfoMapper.getOpsRiskLevels());
		basicInfoMap.put("opsUnexpectEventList", baseInfoMapper.getOpsUnexpectedEvents());
		
		//产科记录
		basicInfoMap.put("fetusPositionList", baseInfoMapper.getfetusPositions());
		basicInfoMap.put("childBearList", baseInfoMapper.getchildBirths());
		
		//医院感染
		basicInfoMap.put("pathogenyList", baseInfoMapper.getPathogenys());
		basicInfoMap.put("pathogenyExemplarList", baseInfoMapper.getPathogenyExemplars());
		basicInfoMap.put("predisposingFactorList", baseInfoMapper.getPredisposingFactors());
		return basicInfoMap;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> queryDiseaseByLikeCode(String diseaseCode) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> diseases = stdDiseaseMapper.queryDiseaseByLikeCode(diseaseCode);
		return diseases;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> queryDiseaseByLikeIndexCode(
			String diseaseIndexCode) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> diseases = stdDiseaseMapper.queryDiseaseByLikeIndexCode(diseaseIndexCode);
		return diseases;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> queryDiseaseByLikeName(String diseaseName) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> diseases = stdDiseaseMapper.queryDiseaseByLikeName(diseaseName);
		return diseases;
	}

	@Override
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String, Object>> queryOperationByLikeCode(String opsCode) {
		return stdOperationMapper.queryOperationByLikeCode(opsCode);
	}

	@Override
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String, Object>> queryOperationByLikeIndexCode(
			String opsIndexCode) {
		return stdOperationMapper.queryOperationByLikeIndexCode(opsIndexCode);
	}

	@Override
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String, Object>> queryOperationByLikeName(String opsName) {
		return stdOperationMapper.queryOperationByLikeName(opsName);
	}

	@Override
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getMedicalOfficesByHospitalId(
			Integer hospitalId) {
		return baseInfoMapper.getMedicalOfficesByHospitalId(hospitalId);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getTreatmentTraceTypes() {
		return baseInfoMapper.getTreatmentTraceTypes();
	}


	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getOutHospitalTypes() {
		return baseInfoMapper.getOutHospitalTypes();
	}

	

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> queryDoctorByLikeName(String doctorName) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> doctors = baseInfoMapper.queryDoctorByLikeName(doctorName);
		return doctors;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> queryDiagnosisSubjectByLikeName(
			String diagnosisSubjectName) {
		// TODO Auto-generated method stub
		List<Map<String,Object>> diagnosisSubjects = baseInfoMapper.queryDiagnosisSubjectByLikeName(diagnosisSubjectName);
		return diagnosisSubjects;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Integer getSexId(String sexName) {
		Integer sexId = null;
		if(StringUtils.isNotBlank(sexName)){
			List<Map<String, Object>> sexs = baseInfo1Mapper.getSexIdsByName(sexName);
			if (!sexs.isEmpty()) {
				sexId = (Integer) sexs.get(0).get("id");
			}
		}
		
		return sexId;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Integer getInDeptId(String inDeptCode) {
		Integer inDeptId = null;
		
		if(inDeptCode != null){
			List<Map<String, Object>> inMedicalOffices = baseInfo1Mapper.getOfficesByOfficeCode(inDeptCode);
			if (!inMedicalOffices.isEmpty()) {
				inDeptId  = (Integer) inMedicalOffices.get(0).get("id");
			}
		}
		
		return inDeptId;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Integer getOutDeptId(String outDeptCode) {
		Integer outDeptId = null;
		
		if(outDeptCode != null){
			List<Map<String, Object>> outMedicalOffices = baseInfo1Mapper.getOfficesByOfficeCode(outDeptCode);
			if (!outMedicalOffices.isEmpty()) {
				outDeptId = (Integer) outMedicalOffices.get(0).get("id");
			}
		}
		
		return outDeptId;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Integer getInHospitalTypeId(String inHospitalTypeCode) {
		Integer inhospitalTypeId = null;
		
		if(StringUtils.isNotBlank(inHospitalTypeCode)){
			List<Map<String, Object>> inHospitalTypes = baseInfo1Mapper.getMysqlInHospitalTypeByCode(inHospitalTypeCode);
			if (!inHospitalTypes.isEmpty()) {
				inhospitalTypeId = (Integer) inHospitalTypes.get(0).get("id");
			}
		}
		
		return inhospitalTypeId;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Integer getOutHospitalTypeId(String outHospitalTypeCode) {
		Integer outHospitalTypeId = null;
		if(StringUtils.isNotBlank(outHospitalTypeCode)){
			List<Map<String, Object>> outHospitalTypes = baseInfo1Mapper.getOutHospitalTypeByCode(outHospitalTypeCode);

			if (!outHospitalTypes.isEmpty()) {
				outHospitalTypeId  = (Integer) outHospitalTypes.get(0).get("id");
			}
		}
		
		return outHospitalTypeId;
	}


	
	
	
	
	
	
}
